﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCCN.DAL;

namespace SCCN.BLL
{
    public class FriendLinksBLL
    {
        private static FriendLinksBLL instance = null;

        /// <summary>
        /// 单例构造
        /// </summary>
        /// <returns></returns>
        public static FriendLinksBLL getInstance()
        {
            if (instance == null)
            {
                instance = new FriendLinksBLL();
            }
            return instance;
        }

        public bool Add(SC_FriendLinks obj)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                entites.AddToSC_FriendLinks(obj);
                return entites.SaveChanges() > 0 ? true : false;
            }
        }

        public bool Update(SC_FriendLinks obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_FriendLinks objSC_FriendLinks = entities.SC_FriendLinks.FirstOrDefault(v => v.Id == obj.Id);
                entities.Detach(objSC_FriendLinks);
                objSC_FriendLinks.FriendLink = obj.FriendLink;
                objSC_FriendLinks.FriendLinkUrl = obj.FriendLinkUrl;
                objSC_FriendLinks.IsDelete = obj.IsDelete;
                objSC_FriendLinks.FLImgUrl = obj.FLImgUrl;
                objSC_FriendLinks.OrderId = obj.OrderId;
                entities.Attach(objSC_FriendLinks);
                var newSC_FriendLinks = entities.ObjectStateManager.GetObjectStateEntry(objSC_FriendLinks);
                newSC_FriendLinks.SetModified();
                newSC_FriendLinks.SetModifiedProperty("FriendLink");
                newSC_FriendLinks.SetModifiedProperty("FriendLinkUrl");
                newSC_FriendLinks.SetModifiedProperty("IsDelete");
                newSC_FriendLinks.SetModifiedProperty("FLImgUrl");
                newSC_FriendLinks.SetModifiedProperty("OrderId");
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public bool Delete(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_FriendLinks obj = entities.SC_FriendLinks.FirstOrDefault(r => r.Id == id);
                obj.IsDelete = true;
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public SC_FriendLinks GetFriendLinksById(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_FriendLinks.FirstOrDefault(r => r.IsDelete == false && r.Id == id);
            }
        }

        /// <summary>
        /// 根据友链名查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public List<SC_FriendLinks> GetSC_FriendLinksByName(string linkName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_FriendLinks.Where(r => r.IsDelete == false && r.FriendLink.Contains(linkName)).ToList<SC_FriendLinks>();
            }
        }

        /// <summary>
        /// 根据条件查询友链
        /// </summary>
        /// <returns></returns>
        public List<SC_FriendLinks> GetFriendLinksByCondition(SC_FriendLinks obj, int pageIndex, int pageSize, out int rowCount)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                rowCount = 0;
                var list = from r in entites.SC_FriendLinks
                           select r;
                if (obj.FriendLink.Length > 0)
                {
                    list = list.Where(a => a.FriendLink.Contains(obj.FriendLink));
                }
                if (obj.FriendLinkUrl.Length > 0)
                {
                    list = list.Where(a => a.FriendLinkUrl == obj.FriendLinkUrl);
                }
                if (obj.IsDelete != null)
                {
                    list = list.Where(a => a.IsDelete == obj.IsDelete);
                }
                if (pageSize > 0)
                {
                    rowCount = list.Count<SC_FriendLinks>();
                    list = list.OrderBy(a => a.OrderId).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                }
                return list.ToList<SC_FriendLinks>();
            }
        }
    }
}
